Skip to content

Fix C harness JSON output for non-finite doubles - #441

Merged
jserv merged 1 commit into
sysprog21:mainfrom
jengyoyoAo:codex/fix-c-nonfinite-json
Oct 23, 2026
Merged

jserv merged 1 commit into
sysprog21:mainfrom
jengyoyoAo:codex/fix-c-nonfinite-json

Conversation

@jengyoyoAo

@jengyoyoAo jengyoyoAo commented Oct 23, 2026 •

Copy link
Copy Markdown

When a C solution returns inf or NaN, the harness prints it directly into the JSON output. This make the whole result invalid, so even passing cases disappear.

This change prints null for non-finite doubles, like the C++ and Java runners already do. It handles both double and double[].

I added four regression tests for infinity, negative infinity, NaN, and mixed arrays. They fail before the fix and pass afterward. All 66 tests in the compiler-explorer test file pass.

The full test suite still has some local failures due to missing assets, plus a C++ test that was terminated. Rerunning the compiler-explorer test file on its own passed.


Summary by cubic

Fixes the C harness so non-finite doubles (inf, -inf, NaN) serialize as null instead of raw inf/nan strings, which were corrupting the entire JSON output and hiding passing results. This makes the C runner consistent with the existing C++ and Java runners. Applies to both double and double[] return types, and adds regression tests for all non-finite cases plus mixed finite/non-finite arrays.

Written for commit 6f84dc7. Summary will update on new commits.

View guided diff

Closes #438

cubic-dev-ai[bot]

This comment was marked as resolved.

@jserv jserv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check CONTRIBUTING.md carefully, run make hooks, and rework git commit messages.

Comment thread web/compiler-explorer.js
static struct Node* nextTree(int* values, bool* nulls, int size) { if (!size || nulls[0]) return NULL; struct Node** nodes = calloc(size, sizeof(*nodes)); for (int i = 0; i < size; i++) if (!nulls[i]) { nodes[i] = calloc(1, sizeof(*nodes[i])); nodes[i]->val = values[i]; } for (int i = 0, child = 1; i < size && child < size; i++) { if (!nodes[i]) continue; if (child < size) nodes[i]->left = nodes[child++]; if (child < size) nodes[i]->right = nodes[child++]; } struct Node* root = nodes[0]; free(nodes); return root; }
static void json_int_array(FILE* out, const int* values, int size) { fputc('[', out); for (int i = 0; i < size; i++) { if (i) fputc(',', out); fprintf(out, "%d", values[i]); } fputc(']', out); }
static void json_double_array(FILE* out, const double* values, int size) { fputc('[', out); for (int i = 0; i < size; i++) { if (i) fputc(',', out); fprintf(out, "%.17g", values[i]); } fputc(']', out); }
static void json_double(FILE* out, double value) { if (isfinite(value)) fprintf(out, "%.17g", value); else fputs("null", out); }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Python runner still has the bug this fixes for C. In web/runners.js the per-case json.dumps(actual) check only catches TypeError/ValueError, and the default allow_nan=True raises neither, so float("inf") reaches the final json.dumps({"results": results}) as bare Infinity and the worker's JSON.parse rejects the whole run. This can be a follow-up: map non-finite floats in actual to None before appending, and add the same powx-n case for Python.

@jengyoyoAo
jengyoyoAo force-pushed the codex/fix-c-nonfinite-json branch from d023cc2 to b262ea6 Compare October 23, 2026 00:31
Non-finite C answers produce invalid JSON and discard results for
every case, including passing ones. Serialize them as null to keep
the run readable and match the C++ and Java runners.
@jengyoyoAo
jengyoyoAo force-pushed the codex/fix-c-nonfinite-json branch from b262ea6 to 6f84dc7 Compare October 23, 2026 00:56
@jserv
jserv requested review from ColtenOuO and alanhc October 23, 2026 01:06
@jserv

jserv commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Issue #438 reports that a C solution returning inf or NaN for a double or double[] problem loses every case result, and proposes the same null helper for both printers that C++ and Java already use. This branch delivers that fix and its powx-n regression case, but its body does not link the issue. Add Closes #438 at the end of the PR body.

@jserv

jserv commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

@jeng-a-yo #438 reports this exact failure, with the Compound Growth Factor reproduction, and is assigned to @peter1001136, who filed it. Since this branch delivers it, the assignee has been asked to review this pull request rather than open a second one. What stands between this branch and a re-review is the Closes #438 line in the body and the commit messages from the earlier changes-requested review.

@jserv
jserv merged commit 8ecf93b into sysprog21:main Oct 23, 2026
10 checks passed
@jserv

jserv commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Thank @jeng-a-yo for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C tests lose every result when the solution returns inf or NaN

2 participants